package com.example.wuqujingdianphone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.adapter.BrowsingHistoryAdapter;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.bean.BookListBean2;
import com.example.wuqujingdianphone.bean.BrowsingHistoryBean;
import com.example.wuqujingdianphone.utils.NiceDialogUtils;
import com.example.wuqujingdianphone.utils.SPUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BrowsingHistoryActivity extends BaseActivity {
    private static final String TAG = "BrowsingHistoryActivity";
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private BrowsingHistoryAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browsing_history);
        ButterKnife.bind(this);
        mTitle.setText("浏览记录");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        initView();
        initData();


    }


    private void initView() {
        //new GridLayoutManager(this, 3)
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BrowsingHistoryAdapter(this, null);
        mRecyclerView.setAdapter(mAdapter);


        /*mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.i(TAG, "onItemClick: position=" + position);
                BrowsingHistoryBean item = mAdapter.getItem(position);
                if (item.fname != null) {
                    Intent intent;
                    switch (item.fname) {
                        case "图书":
                            intent = new Intent(BrowsingHistoryActivity.this, BookDetailActivity.class);
                            intent.putExtra("bookId", mAdapter.getItem(position).id);
                            intent.putExtra("bookTitle", mAdapter.getItem(position).bname);
                            startActivity(intent);

                            ToastUtil.show("图书id=" + mAdapter.getItem(position).id);

                            break;
                        case "听书":
                            intent = new Intent(BrowsingHistoryActivity.this, BookWebDetailActivity.class);
                            intent.putExtra("type", "听书");
                            intent.putExtra("bookId", mAdapter.getItem(position).id);
                            startActivity(intent);
                            break;
                        case "视频":
                            intent = new Intent(BrowsingHistoryActivity.this, BookWebDetailActivity.class);
                            intent.putExtra("type", "视频");
                            intent.putExtra("bookId", mAdapter.getItem(position).id);
                            startActivity(intent);

                            break;

                        case "阅读活动":
                            intent = new Intent(BrowsingHistoryActivity.this, BookWebDetailActivity.class);
                            intent.putExtra("type", "阅读活动");
                            intent.putExtra("bookId", mAdapter.getItem(position).id);
                            startActivity(intent);

                            break;


                    }


                }


            }
        });*/


    }

    private void initData() {


        showLoading(this);

        //http://wenqujingdian.com/index.php/Jiekou/look/username/wwwwww
        String account = SPUtil.getString(this, GlobalConfig.ACCOUNT_KEY, "");
        Log.i(TAG, "initData: account=" + account);

        ViseHttp.GET("index.php/Jiekou/look/username/" + account)
                .tag(TAG)
                // .addParam("", account)
                .request(new ACallback<List<BrowsingHistoryBean>>() {
                    @Override
                    public void onSuccess(List<BrowsingHistoryBean> bookListBeans) {
                        cancelLoading();
                        // Log.i(TAG, "onSuccess: bookListBeans.size=" + bookListBeans.size() + " " + bookListBeans.get(0).url);
                        if (bookListBeans != null && bookListBeans.size() > 0) {
                            mAdapter.setNewData(bookListBeans);
                        } else {
                            Log.i(TAG, "onSuccess: bookListBeans is null");
                        }

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        cancelLoading();
                        ToastUtil.show("errCode=" + errCode + "　errMsg＝" + errMsg);
                    }
                });


    }
}
